<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{/layout}">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=0">

    <title>修改密码</title>


    <div th:replace="commons/css/css_personal::personal_css"></div>
    <link th:href="@{/personal/css/stepstyle.css}" rel="stylesheet" type="text/css">
    <div th:replace="commons/css/css_header_footer :: public_css1"></div>
    <div th:replace="commons/js/js_header_footer :: public_js1"></div>
    <div th:replace="commons/js/js_personal::personal_js"></div>
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery.bootstrapvalidator/0.5.3/css/bootstrapValidator.min.css">
    <script src="https://cdn.bootcss.com/jquery.bootstrapvalidator/0.5.3/js/bootstrapValidator.min.js"></script>
</head>

<body>
<!--头 -->
<div th:replace="commons/header::public_header"></div>

<!-- Preloader -->
<div id="preloader" class="preloader">
    <div class="loader-cube">
        <div class="loader-cube__item1 loader-cube__item"></div>
        <div class="loader-cube__item2 loader-cube__item"></div>
        <div class="loader-cube__item4 loader-cube__item"></div>
        <div class="loader-cube__item3 loader-cube__item"></div>
    </div>
</div>
<!-- End Preloader -->

<b class="line"></b>
<div class="center">
    <div class="col-main">
        <div class="main-wrap">

            <div class="am-cf am-padding">
                <div class="am-fl am-cf"><strong class="am-text-danger am-text-lg">修改密码</strong> /
                    <small>Password</small>
                </div>
            </div>
            <hr/>
            <!--进度条-->
            <div class="m-progress">
                <div class="m-progress-list">
							<span class="step-1 step">
                                <em class="u-progress-stage-bg"></em>
                                <i class="u-stage-icon-inner">1<em class="bg"></em></i>
                                <p class="stage-name">重置密码</p>
                            </span>
                    <span class="step-2 step">
                                <em class="u-progress-stage-bg"></em>
                                <i class="u-stage-icon-inner">2<em class="bg"></em></i>
                                <p class="stage-name">完成</p>
                            </span>
                    <span class="u-progress-placeholder"></span>
                </div>
                <div class="u-progress-bar total-steps-2">
                    <div class="u-progress-bar-inner"></div>
                </div>
            </div>
            <form id="editPsw" class="am-form am-form-horizontal">
                <input type="hidden" id="user-id" name="userid" th:value="${session.user.userid}">
                <div class="form-group">
                    <div class="am-form-group">
                        <label for="user-old-password" class="am-form-label">原密码</label>
                        <div class="am-form-content">
                            <input type="password" id="user-old-password" name="oldpassword" placeholder="请输入原登录密码">
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="am-form-group">
                        <label for="user-new-password" class="am-form-label">新密码</label>
                        <div class="am-form-content">
                            <input type="password" id="user-new-password" name="newpassword" placeholder="由数字、字母组合">
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="am-form-group">
                        <label for="user-confirm-password" class="am-form-label">确认密码</label>
                        <div class="am-form-content">
                            <input type="password" id="user-confirm-password" name="repassword"
                                   placeholder="请再次输入上面的密码">
                        </div>
                    </div>
                </div>
                <div class="info-btn">
                    <input type="button" class="am-btn am-btn-danger" id="edit" value="保存修改">
                </div>

            </form>

        </div>
        <!--底部-->
        <div th:replace="commons/personal_footer::personal_footer"></div>
    </div>

    <div th:replace="commons/personal_footer::personal_menu"></div>
</div>

<script>
    $(document).ready(function () {
        changeStatus();
    })

    function changeStatus() {
        $("#li_secure").addClass("active");
    }

    $("#edit").click(function () {
        // 表单验证
        $('#editPsw').bootstrapValidator({
            message:'信息输入错误',
            feedbackIcons:{
                valid:'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields:{
                oldpassword:{
                    validators:{
                        notEmpty:{
                            message:'原密码不能为空'
                        }
                    }

                },
                newpassword:{
                    validators:{
                        notEmpty:{
                            message:'新密码不能为空'
                        },
                        identical: {
                            field: 'repassword',
                            message: '用户新密码与确认密码不一致！'
                        },
                    }
                },
                repassword:{
                    validators:{
                        notEmpty:{
                            message:'二次输入不能为空'
                        },
                        identical: {
                            field: 'newpassword',
                            message: '用户新密码与确认密码不一致！'
                        },
                    }
                }

            }

        });
        //校验数据
        $('#editPsw').data('bootstrapValidator').validate();
        if(!$('#editPsw').data('bootstrapValidator').isValid()){
            return;
        }

        var oldpassword = $('#user-old-password').val();
        var newpassword = $('#user-new-password').val();
        var userid = $('#user-id').val();

        $.ajax({
            type:'POST',
            url:"/users/editCheck",
            data:{
                userid: userid,
                password: oldpassword,
            },
            success:function (data) {
                if(data.code==100){
                    $.ajax({
                        type:'POST',
                        url: "/users/editPassword",
                        data:{
                            userid: userid,
                            password: newpassword,
                        },
                        success:function (data) {
                            swal({
                                    title: data.msg,
                                    type:'success',
                                    icon: "success",
                                    showCancelButton:false,
                                    confirmButtonText: '',
                                    closeOnConfirm: false,
                                    closeOnCancel: false
                                },
                                function (isConfirm) {
                                    if(isConfirm){
                                        window.location.href="/users/login";
                                    }
                                });
                        }
                    });
                }else{
                    swal(data.msg);
                }
            }
        });
    })


</script>

</body>

</html>